<?php
namespace Crm\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Expression;
class RechargeCardTable extends AbstractTable
{
    protected $table = 'hy_recharge_card';
    protected $id = 'vt_id';
    
    
    public function getRechargeCardList($query = array())
    {
        $select = new Select(array('rc' =>$this->getTable()));
        
        $where = new Where();
        if(!empty($query['name'])) {
            $where->like('rc.rc_name', '%'.$query['name'].'%');
        }
//         if(!empty($query['start_time'])) {
//             $where->greaterThan('vt.vt_start_time', $query['start_time']);
//         }
//         if(!empty($query['end_time'])) {
//             $where->lessThan('vt.vt_end_time', $query['end_time']);
//         }

        $select->where($where);
        
        $select->order('rc.rc_create_time DESC');
        return $this->fetchPaginate($select,$query);
    }
    public function getByRechargeId($rc_id)
    {
        $rowset = $this->select(array('rc_id' => $rc_id));
        $row = $rowset->current();
        return $row;
    }

    public function updateItem($id,$data){
        if(empty($id)){
            throw new \Exception("id field is invalid!");
        }
        $item = array(
            'rc_name'=>$data['name'],
            'rc_amount'=>$data['amount'],
            'rc_start_time'=>$data['start_time'],
            'rc_end_time'=>$data['end_time'],
        );
        if(!empty($data['remark'])) {
            $item['rc_remark']=$data['remark'];
        }
        return $this->update($item,array('rc_id'=>$id));
    }
    
    public function addItem($data){
    
        $item = array(
            'rc_name'=>$data['name'],
            'rc_amount'=>$data['amount'],
            'rc_num'=>$data['num'],
            'rc_start_time'=>$data['start_time'],
            'rc_end_time'=>$data['end_time'],
            'rc_create_time'=>new Expression('NOW()'),
    
        );
        if(!empty($data['remark'])) {
            $item['rc_remark']=$data['remark'];
        }
        $this->insert($item);
        $id = $this->lastInsertValue;
        return $id;
    }
    public function deleteItem($id){
        if(empty($id)){
            throw new \Exception("id field is invalid!");
        }
        return $this->delete(array('rc_id'=>$id));
    
    }
    public function getOutExcelList()
    {
        $select = new Select(array('rc' =>$this->getTable()));
   
        $select->order('rc.rc_id');
         
        $rowset = $this->selectWith($select);
        $list = $rowset->toArray();
    
        return $list;
    }
}